package com.spider.pool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author β世界
 * Created on 13:47
 * @Description URI的链接池，包含了未处理的链接和待处理的链接
 */
@SuppressWarnings("unused")
public class UriQueueScheduler {
    private final Logger logger = LoggerFactory.getLogger(UriQueueScheduler.class);

    /** 未处理链接队列 **/
    private final Queue<URI> untreatedQueue = new LinkedBlockingQueue<>();
    private final HashSet<URI> keySet = new HashSet<>();

    /** 已处理的链接队列 **/
    private final List<URI> process = new ArrayList<>();

    /**
     * @author β世界 on 13:56 2020/2/8
     * @Description 添加未处理的URL到线程池中
     * @param url:需要添加的URL
     **/
    public synchronized void addUrl(String url){
        try {
            URI uri = new URI(url);
            // 判断是否有重复的链接
            if(!keySet.contains(uri)){
                untreatedQueue.add(uri);
                keySet.add(uri);
                logger.info("添加待处理链接: "+uri);
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author β世界 on 14:00 2020/2/8
     * @Description 获取一个未处理的URI
     * @return java.net.URI
     **/
    public synchronized URI getUntreatedUrl(){
        return untreatedQueue.poll();
    }

    /**
     * @author β世界 on 18:04 2020/2/22
     * @Description 检查当前未处理URL池是否空了
     * @return int
     **/
    public synchronized int getUntreatedSize(){
        return untreatedQueue.size();
    }

    /**
     * @author β世界 on 22:41 2021/5/6
     * @Description 返回所有已经处理的URI
     * @return java.util.List<java.net.URI>
     **/
    @SuppressWarnings("unused ")
    public List<URI> getProcessURI(){
        return process;
    }

    /**
     * @author β世界 on 13:35 2020/7/11
     * @Description 关闭ProcessURL
     **/
    public synchronized void clearProcessed(){
        process.clear();
    }

    /**
     * 获取所有已经处理的URI
     * @author Administratorr
     * @date 15:48 2021/6/21
     * @return java.util.List<java.net.URI>
     **/
    public List<URI> getAllProcessURI(){
        return process;
    }

    /**
     * 获取所有的URI
     * @author Administratorr
     * @date 15:49 2021/6/21
     * @return java.util.List<java.net.URI>
     **/
    public List<URI> getAllURI(){
        List<URI> untreated = new ArrayList<>(untreatedQueue);
        List<URI> resultList = new ArrayList<>();
        resultList.addAll(untreated);
        resultList.addAll(process);
        return resultList;
    }

    /**
     * @author β世界 on 13:35 2020/7/11
     * @Description 关闭UntreatedURL
     **/
    public synchronized void clearUntreated(){
        untreatedQueue.clear();
        keySet.clear();
    }

}
